package com.blb.exception;

import com.alibaba.fastjson.JSONObject;
import com.blb.utils.HttpUtils;
import com.blb.utils.ResponseHelp;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

@Component
public class MyExceptionHandler extends SimpleMappingExceptionResolver {
    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        response.setCharacterEncoding("utf-8");
        ex.printStackTrace();
        if (HttpUtils.isAjaxRequest(request)) {
            System.out.println("-----");
            if (ex instanceof UnauthorizedException) {
                catchEx(response,"403, 您无权限访问此资源");
            }else if(ex instanceof ArithmeticException){
                catchEx(response,"500, 算术异常");
            }else if(ex instanceof NullPointerException){
                catchEx(response,"500,空指针异常");
            }else if(ex instanceof IndexOutOfBoundsException){
                catchEx(response,"500,下标越界异常");
            }else if(ex instanceof ClassCastException){
                catchEx(response,"500,类型强制转换异常");
            }else if(ex instanceof SQLException){
                catchEx(response,"500,操作数据库异常");
            }else if(ex instanceof NumberFormatException){
                catchEx(response,"500,字符串转换为数字异常");
            }else if(ex instanceof IllegalStateException){
                catchEx(response,"500,请求状态异常");
            }else if(ex instanceof IllegalArgumentException){
                catchEx(response,"500,非法参数异常");
            }else if(ex instanceof FileNotFoundException){
                catchEx(response,"404,文件未找到异常");
            }
        }else{
            if (ex instanceof UnauthorizedException) {
                return new ModelAndView("error/403");
            }else if(ex instanceof ArithmeticException){
                return new ModelAndView("error/500");
            }else if(ex instanceof NullPointerException){
                return new ModelAndView("error/500");
            }else if(ex instanceof IndexOutOfBoundsException){
                return new ModelAndView("error/500");
            }else if(ex instanceof ClassCastException){
                return new ModelAndView("error/500");
            }else if(ex instanceof SQLException){
                return new ModelAndView("error/500");
            }else if(ex instanceof NumberFormatException){
                return new ModelAndView("error/500");
            }else if(ex instanceof IllegalStateException){
                return new ModelAndView("error/500");
            }else if(ex instanceof IllegalArgumentException){
                return new ModelAndView("error/403");
            }else if(ex instanceof FileNotFoundException){
                return new ModelAndView("error/404");
            }
        }
        return new ModelAndView();
    }

    public void catchEx(HttpServletResponse response,String errMsg){
        ResponseHelp responseHelp = ResponseHelp.error(errMsg);
        String jsonString = JSONObject.toJSONString(responseHelp);
        try {
            response.getWriter().write(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}